/*
 * 
 * 
 * 
 */
package com.hboxs.asl.entity;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.hboxs.asl.Constant;

import javax.persistence.*;
import java.util.*;

/**
 * Entity - 银行卡
 */
@Entity
@Table(name = "xx_bank_card")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_bank_card_sequence")
public class BankCard extends OrderEntity {

    private static final long serialVersionUID = 2673602123029665976L;

    public static List<String> imageIcons = null;

    static {
        String[] imageIconStr = {"abc", "bob", "boc", "bosh", "ccb", "ceb", "cib", "citic", "cmb", "cmbc", "comm", "gdb", "hxb", "icbc", "nbcb", "pab", "psbc", "sdb", "spdb"};
        imageIcons = Arrays.asList(imageIconStr);
    }

    /**
     * 会员
     */
    private Member member;

    /**
     * 真实姓名
     */
    private String name;

    /**
     * 卡号
     */
    private String cardNumber;

    /**
     * 开户行
     */
    private String subBranch;

    /**
     * 开户支行
     */
    private String openingBranch;

    /**
     * 预留手机号码
     */
    private String phone;

    /**
     * 银行卡英文缩写
     */
    private String code;

    private enum Type {

        // 储蓄卡
        deposit,

        // 信用卡
        credit;

        private static Map<String, Type> map = new HashMap<>();

        static {
            map.put("储蓄卡", deposit);
            map.put("信用卡", credit);
        }

        @JsonCreator
        public static Type forValue(String value) {
            return map.get(value);
        }

        @JsonValue
        public String toValue() {
            for (Map.Entry<String, Type> entry : map.entrySet()) {
                if (entry.getValue() == this) {
                    return entry.getKey();
                }
            }

            return "";
        }
    }

    /**
     * 卡型
     */
    private Type type;

    /**
     * 获取会员
     *
     * @return 会员
     */
    @ManyToOne(fetch = FetchType.LAZY)
    public Member getMember() {
        return member;
    }

    /**
     * 设置会员
     *
     * @param member 会员
     */
    public void setMember(Member member) {
        this.member = member;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getSubBranch() {
        return subBranch;
    }

    public void setSubBranch(String subBranch) {
        this.subBranch = subBranch;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Transient
    public String getIcon() {
        String icon = "";
        if (this.code != null && BankCard.imageIcons.contains(code.toLowerCase())) {
            icon = Constant.BANK_ICON_PATH + "/" + this.code.toLowerCase() + ".png";
        } else {
            icon = Constant.BANK_ICON_PATH + "/" + "default@2x.png";
        }
        return icon;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getOpeningBranch() {
        return openingBranch;
    }

    public void setOpeningBranch(String openingBranch) {
        this.openingBranch = openingBranch;
    }
}